-- UIGroupBonusDesc
-- Create by huanghr
-- 团购奖励物品信息界面

-- UIGroupBonusDesc继承自Layer
UIGroupBonusDesc = class("UIGroupBonusDesc", function()
    return cc.Layer:create();
end);

function UIGroupBonusDesc.create(bonus, goodsId, level)
    return UIGroupBonusDesc.new(bonus, goodsId, level);
end

-- 构造函数
function UIGroupBonusDesc:ctor(bonus, goodsId, level)
    self:setName("UIGroupBonusDesc");
    self.bonus = bonus;

    self.goodsId = goodsId;
    self.level = level;

    -- 初始化
    self:setLocalZOrder(UIMgr.BONUS_NOTIFY_ZORDER + 1);
    local node = cc.CSLoader:createNode("layout/market/BonusItemDesc.csb");
    self:addChild(node);
    self.node = node;

    -- 防止点击事件被背景吞掉
    self:setTouches();

    -- 重绘
    self:redraw();

    -- 注册点击事件
    self:registerTouchEvent();

    -- 适配
    self:resize();
end

-- 注册点击事件
function UIGroupBonusDesc:registerTouchEvent()
    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UIGroupBonusDesc");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    local btnBgBack = findChildByName(self.node, "bg/bgImg");
    local function onBgBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UIGroupBonusDesc");
        end
    end
    btnBgBack:addTouchEventListener(onBgBackClick);
end

-- 重绘
function UIGroupBonusDesc:redraw()
    local bonus = self.bonus;

    local nameLabel = findChildByName(self.node, "CT/name");
    local descLabel = findChildByName(self.node, "CT/desc");
    local detailLabel = findChildByName(self.node, "CT/effect_desc");
    local progressLabel = findChildByName(self.node, "CT/group_buy_tip");
    local iconImg = findChildByName(self.node, "CT/icon");

    if bonus[1] == 1 then
        -- 物品奖励

        -- 道具名称
        local itemId = bonus[2];
        TextStyleM.setTextStyle(nameLabel,TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_DARK_RED);
        nameLabel:setString(ItemM.query(itemId, "name"));

        -- 道具的图标
        local iconName = ItemM.query(itemId, "icon");
        local iconPath = getItemIconPath(iconName);
        iconImg:loadTexture(iconPath);
        iconImg:setOpacity(80);

        -- 道具描述
        TextStyleM.setTextStyle(descLabel,TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
        descLabel = applyString(descLabel, ItemM.getItemDesc(itemId));

        -- 显示奖励进度

        local str = "";

        -- 计算当团购奖励需要人数
        local needPeople = GroupPurchaseM.getRequire(self.goodsId, self.level);
        str = string.format(getLocStr("cur_require_people"), needPeople);

        -- 购买过该冈布奥的人数
        local hasBuy = GroupPurchaseM.fetchProgress(self.goodsId);
        local progress = 0;
        if hasBuy ~= nil then
            progress = hasBuy.progress;
        end

        local str2 = string.format(getLocStr("cur_has_buy_amount"), progress);

        TextStyleM.setTextStyle(progressLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_GREEN);
        progressLabel = applyString(progressLabel, str.."\n"..str2);

        local group = ItemM.query(itemId, "group");

        -- 需要描述的信息Item类型
        local needDetail = {12, 13, 25, 16, 28, 27};

        if table.indexOf(needDetail, group) ~= -1 then
            -- 道具详细信息

            local detailDesc = ItemM.getItemDetail(itemId);

            -- group为28可携带迷宫的特殊道具，27是试剂配方
            if group == 28 or group == 27 then
                -- 本身不是蓝色，需要适应
                detailDesc = "[blue]" .. detailDesc .. "[-]";
            end

            TextStyleM.setTextStyle(detailLabel,TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
            detailLabel = applyString(detailLabel, detailDesc);
        else
            detailLabel:setVisible(false);
        end
    end

    -- 自适应所有组件
    local bg = findChildByName(self.node, "CT/bg");
    AlignM.adaptLayout(bg, AlignM.BG_SIZE_SMALL,
        { nameLabel, descLabel, detailLabel, progressLabel });

    -- 修正一下物品图标的位置
    iconImg:setPositionY(nameLabel:getPositionY() - 20);
end

function UIGroupBonusDesc:setTouches()
    local bg = findChildByName(self.node, "CT/bg");

    for i = 1, 4 do
        local subBg = findChildByName(bg, "bg" .. i);
        if subBg ~= nil then
            subBg:setSwallowTouches(false);
        end
    end
end

-- 适配
function UIGroupBonusDesc:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end